<?php
$my_yamii_lbl = BUtils::T('my_home_lbl');

$this->pageTitle = $my_yamii_lbl;

$points = Yii::app()->locale->numberFormatter->formatDecimal($row['points']);
$amount = Yii::app()->locale->numberFormatter->formatDecimal($row['amount']);

$head_photo_url = empty($row['picture']) ? Yii::app()->baseUrl . '/img/user-128.png' : $row['picture'];
$status = Yii::app()->user->getState('status');
?>
<div class="section-wrapper post-w">
<div class="container">
	<div class="row">
		<div class="span8">
			<div class="white-card text-center">
				<i class="icon-trophy icon-5x color-reward"></i>
<?php if (1 != $status) {
	echo '<p><a href="#myStatus" role="button" class="btn btn-danger btn-large" data-toggle="modal">' . Yii::t('app', 'Activate your points') . '</a></p>'
		. '<p>' . Yii::t('app', 'Please enter the phone number/email that you have used to earn points at stores.') . '</p>';
} else {
	if (!empty(Yii::app()->user->mobile)) {
		echo '<p>' . Yii::t('app', 'Your active phone number') . ': <strong style="font-size:18px;">'.Yii::app()->user->mobile.'</strong></p>'
			. '<p>'. Yii::t('app', 'Use this phone number at any Yamii location to earn rewards.') .'</p>';
	} elseif (!empty(Yii::app()->user->email)) {
		echo '<p>' . Yii::t('app', 'Your active email') . ': <strong style="font-size:18px;">'.Yii::app()->user->email.'</strong></p>'
			. '<p>'. Yii::t('app', 'Use this email at any Yamii location to earn rewards.') .'</p>';
	}
} ?>
			</div>
			<div class="white-card">
				<h4><?php echo Yii::t('app', 'You have earned points at these stores.'); ?></h4>
				<div id="tab-merchants"></div>
			</div>
		</div>
		<div class="span4">
			<div class="white-card no-padding">
				<div class="profile-box" style="margin:15px 0;">
					<div class="pull-left" style="margin:0 12px;">
						<div class="user-face"><img style="width:64px;" src="<?php echo $head_photo_url; ?>" /></div>
					</div>
					<div class="pull-left">
						<ul class="icons-ul">
							<li style="margin-bottom: 12px;"><?php echo Yii::app()->user->name; ?></li>
							<li class="add-location">
								<i class="icon-li icon-edit"></i>
								<a style="font-size:13px;" href="<?php echo Yii::app()->createUrl('/user/profile'); ?>">Edit profile</a>
							</li>
						</ul>
					</div>
					<div class="clearfix"></div>
				</div>
				<div class="userprofile row-fluid hidden">
					<div class="stat span4">
						<div class="num" id="current_pts">0</div>
						<div class="num-des">Lifetime Points</div>
					</div>
					<div class="stat span4">
						<div class="num" id="spend_pts">0</div>
						<div class="num-des">Rewards Redeemed</div>
					</div>
					<div class="stat span4">
						<div class="num" id="stores_cnt">0</div>
						<div class="num-des">Stores</div>
					</div>
				</div>
			</div>
			<div class="white-card no-padding info-box hide">
				<div class="head">You have 2 bonus points!</div>
				<div class="body">
					<p>4</p>
				</div>
				<div class="foot">Claim</div>
			</div>
			<div class="white-card no-padding info-box hide">
				<div class="head">Earn Points Faster</div>
				<div class="body">
					<p>
						<a href="javascript:;" class="facebookButton btn-facebook"><?php echo Yii::t('app', 'Connect with Facebook'); ?></a>
					</p>
					<p>Earn a bonus point for every three check-ins and after every reward.</p>
				</div>
			</div>
		</div>
	</div>
</div>
</div>
<?php
$name = Yii::app()->user->getState('name');
if (empty($name)) { ?>
<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
		<h3>Welcome to Yamii!</h3>
	</div>
	<div class="modal-body">
		<p>Enter your name:</p>
		<?php echo CHtml::textField('user_name', '', array('id'=>'user_name', 'style'=>'width:90%;')) ?>
		<p class="help-block error-info hide">Please enter your name!</p>
	</div>
	<div class="modal-footer">
		<button class="btn btn-primary" id="save_name"><?php echo Yii::t('app', 'Save'); ?></button>
	</div>
</div>
<?php } if (1 != $status) { ?>
<!-- Modal -->
<div id="myStatus" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
		<h4><?php echo Yii::t('app', 'Activate earned points'); ?>:</h4>
	</div>
	<div class="modal-body">
		<p>Please enter phone number/email that you have used to earn points.</p>
		<?php echo CHtml::textField('user_input', '', array('id'=>'user_input', 'style'=>'width:90%;')) ?>
		<p class="help-block error-info hide" id="user_input_error"></p>
	</div>
	<div class="modal-footer">
		<button class="btn btn-primary" id="save_user_input"><?php echo Yii::t('app', 'Activate'); ?></button>
	</div>
</div>
<?php } ?>

<script>
$(function() {
	var _panel = $('#tab-merchants');
	$.ajax({
		url: '<?php echo Yii::app()->createUrl('/user/merchants'); ?>',
		beforeSend: function() {
			_panel.html('<div class="text-center"><img src="<?php echo Yii::app()->baseUrl; ?>/img/loader.gif" /></div>');
		},
		error: function(jqXHR) {
			_panel.html(jqXHR.responseText);
		},
		success: function(html) {
			_panel.html(html);
		}
	});
	
	//if there is no user name, then popup a form to input user name
	$('#myModal').modal({keyboard:false,backdrop:'static'});
	$('#save_name').click(function(){
		$('.help-block.error-info').hide();
		var user_name = $('#user_name').val();
		if (user_name !== '') {
			$.post('<?php echo Yii::app()->createUrl('/user/update'); ?>', {user_name:user_name}, function(data){
				if (data.code === 200) {
					location.reload();
				} else {
					$('.help-block.error-info').show();
				}
			}, 'json');
		} else {
			$('.help-block.error-info').show();
		}
	});
	$('#user_name').focus(function(){
		$('.help-block.error-info').hide();
	});
	
	//if there is active membership button, then popup
	$('#myStatus').modal({keyboard:false,backdrop:'static'});
	$('#save_user_input').click(function(){
		var user_input = $('#user_input').val();
		if (user_input !== '') {
			$.post('<?php echo Yii::app()->createUrl('/user/activate'); ?>', {user_input:user_input}, function(data){
				if (data.code === 200) {
					location.reload();
				} else {
					$('#user_input_error').show().html(data.message);
				}
			}, 'json');
		} else {
			$('#user_input_error').show().html('<?php echo Yii::t('app', 'Please enter phone number or email!'); ?>');
		}
	});
	$('#user_input').focus(function(){
		$('#user_input_error').hide();
	});
	
	//check the points
	/*
	$.get('<?php echo Yii::app()->createUrl('/user/points'); ?>', null, function(data){
		if (data.code === 200) {
			$('#current_pts').html(data.message.current_pts);
			$('#spend_pts').html(data.message.spend_pts);
			$('#stores_cnt').html(data.message.stores);
		}
	}, 'json');*/
});
</script>